import json

from conf.neo4j_cnx import NeoCnxPool

neo_cnx = NeoCnxPool()


class ExportChestGraph:

    @staticmethod
    def tranverse():
        def find_next(uid):
            with neo_cnx.get_session() as session:
                query = (
                    'MATCH (n1:Neo4jNode {uid: $uid}) '
                    'MATCH (n1)-[]->(n2) '
                    'RETURN n2'
                )
                results = session.run(query, uid=uid)
                return_list = []
                for record in results:
                    node = record['n2']
                    return_list.append({'name': node['name'],
                                        'uid': node['uid'],
                                        'children': find_next(node['uid'])})
                return return_list

        uid = '1373817638226829312'
        name = '胸部后前位片'

        return {'name': name, 'uid': uid, 'children': find_next(uid)}


if __name__ == '__main__':
    app = ExportChestGraph()
    graph = app.tranverse()
    print(json.dumps(graph, ensure_ascii=False))
